//
//  YWUploadFailedRecordCell.m
//  maintain
//
//  Created by kyo on 17/1/9.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWUploadFailedRecordCell.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "YWCommonTool.h"
#import "Masonry.h"

#import "YWMission.h"
#import "YWPatrolPointRecord.h"
#import "YWInspectionEquipmentRecord.h"
#import "YWEquipmentMaintenanceRecord.h"
#import "YWEquipmentTroubleRecord.h"
#import "YWMeterReadingRecord.h"
#import "YWAttendanceRecord.h"
#import "YWReportTroubleModel.h"
#import "YWTroubleMissionModel.h"
#import "YWTroubleMissionManager.h"

#import "YWEquipmentManager.h"
#import "YWEquipment.h"
#import "YWBuildingManager.h"
#import "YWInspectionMissionManager.h"
#import "YWKeepWatchMissionManager.h"
#import "YWMissionManager.h"

@interface YWUploadFailedRecordCell ()

@property (nonatomic, weak) UILabel *equipmentLabel;
@property (nonatomic, weak) UILabel *locationLabel;
@property (nonatomic, weak) UILabel *timeLabel;
@end

@implementation YWUploadFailedRecordCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"YWUploadFailedRecordCellIdentifier";
    YWUploadFailedRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YWUploadFailedRecordCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
    }
    return self;
}

- (void)setRecordObject:(id)recordObject{
    _recordObject = recordObject;
    NSString *equipmentName = nil;
    NSString *location = nil;
    NSString *timeString = nil;
    if ([recordObject isKindOfClass:[YWReportTroubleModel class]]) {
        YWReportTroubleModel *reportTrouble = (YWReportTroubleModel *)recordObject;
        if (reportTrouble.equipment.equipmentName || reportTrouble.equipment.equipmentId) {
            equipmentName = reportTrouble.equipment.equipmentName?:NSLocalizedString(@"none", nil);//([[YWEquipmentManager shareManager] equipmentWithId:reportTrouble.equipment.equipmentId].equipmentName?:@"无");
        }else if (reportTrouble.equipment.equipmentTypeId){
            equipmentName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:reportTrouble.equipment.equipmentTypeId];
        }
        equipmentName = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"equipment", nil),equipmentName.length?equipmentName:NSLocalizedString(@"none", nil)];
        
        if (reportTrouble.equipment.equipmentId.length) {
            YWEquipment* equipment = reportTrouble.equipment;//[[YWEquipmentManager shareManager] equipmentWithId:reportTrouble.equipment.equipmentId];
            location = equipment ? [[YWBuildingManager shareManager] equipmentLocation:equipment] : nil;
        }
        if (location.length==0 && reportTrouble.location.locationId) {
            location = [[YWBuildingManager shareManager] areaLocationWithLocationId:reportTrouble.location.locationId];
        }
        location = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"location", nil),location.length?location:NSLocalizedString(@"none", nil)];
        timeString = [YWCommonTool showTime:reportTrouble.reportTime/1000 andFormatter:KTimeFormatShortTimeDate];
    }else if ([recordObject isKindOfClass:[YWPatrolPointRecord class]]){
        YWPatrolPointRecord *pointRecord = (YWPatrolPointRecord*)recordObject;
        if (pointRecord.patrolLineId==0) {
            equipmentName = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"line", nil),NSLocalizedString(@"tempKeepWatch", nil)];
        }else{
            YWPatrolLine *lineModel = [[YWKeepWatchMissionManager shareManager] patrolLineWithPatrolLineId:pointRecord.patrolLineId];
            equipmentName = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"line", nil),lineModel.patrolLineName?:NSLocalizedString(@"none", nil)];
        }
        location = pointRecord.locationId ? [[YWBuildingManager shareManager] areaLocationWithLocationId:pointRecord.locationId] : nil;
        location = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"location", nil),location.length?location:NSLocalizedString(@"none", nil)];
        timeString = [YWCommonTool showTime:pointRecord.patrolTime/1000 andFormatter:KTimeFormatShortTimeDate];
    }else if ([recordObject isKindOfClass:[YWInspectionEquipmentRecord class]]){
        YWInspectionEquipmentRecord *inspectionRecord = (YWInspectionEquipmentRecord*)recordObject;
        if (inspectionRecord.recordType == YWInspectionEquipmentRecordTypeEquipment) {
            equipmentName = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"equipment", nil),inspectionRecord.equipment.equipmentName?:NSLocalizedString(@"none", nil)];
            location = inspectionRecord.equipment ? [[YWBuildingManager shareManager] equipmentLocation:inspectionRecord.equipment] : nil;
            location = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"location", nil),location.length?location:NSLocalizedString(@"none", nil)];
        }else{
            if (inspectionRecord.inspectionLineId==0) {
                equipmentName = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"line", nil),NSLocalizedString(@"tempInspection", nil)];
            }else{
                YWInspectionLineModel *lineModel = [[YWInspectionMissionManager shareManager] inspectionLineWithLineId:inspectionRecord.inspectionLineId];
                equipmentName = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"line", nil),lineModel.inspectionLineName?:NSLocalizedString(@"none", nil)];
            }
            location = inspectionRecord.locationId ? [[YWBuildingManager shareManager] areaLocationWithLocationId:inspectionRecord.locationId] : nil;
            location = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"location", nil),location.length?location:NSLocalizedString(@"none", nil)];
        }
        timeString = [YWCommonTool showTime:inspectionRecord.executeTime/1000 andFormatter:KTimeFormatShortTimeDate];
        
    }else if ([recordObject isKindOfClass:[YWEquipmentTroubleRecord class]]){
        YWEquipmentTroubleRecord *trobleRecord = (YWEquipmentTroubleRecord*)recordObject;
        if (trobleRecord.equipmentId) {
            equipmentName = [[YWEquipmentManager shareManager] equipmentWithId:trobleRecord.equipmentId].equipmentName;
        }else if (trobleRecord.equipmentTypeId){
            equipmentName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:trobleRecord.equipmentTypeId];
        }
        equipmentName = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"equipment", nil),equipmentName.length?equipmentName:NSLocalizedString(@"none", nil)];
        if (trobleRecord.equipmentId.length) {
            YWEquipment* equipment = [[YWEquipmentManager shareManager] equipmentWithId:trobleRecord.equipmentId];
            location = equipment ? [[YWBuildingManager shareManager] equipmentLocation:equipment] : nil;
        }
        if (location.length==0 && trobleRecord.qrcodeId) {
            NSInteger locationId = [[YWEquipmentManager shareManager] locationIdWithQrcodeId:trobleRecord.qrcodeId];
            location = [[YWBuildingManager shareManager] areaLocationWithLocationId:locationId];
        }
        if (location.length==0 && trobleRecord.troubleId) {
            YWTroubleMissionModel * troubleMissionModel = [[YWTroubleMissionManager shareManager] troubleMissionModelWithTroubleId:trobleRecord.troubleId];
            location = [[YWBuildingManager shareManager] areaLocationWithLocationId:troubleMissionModel.trouble.location.locationId];
        }
        location = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"location", nil),location.length?location:NSLocalizedString(@"none", nil)];
        timeString = [YWCommonTool showTime:trobleRecord.completeTime/1000 andFormatter:KTimeFormatShortTimeDate];
        
    }else if ([recordObject isKindOfClass:[YWEquipmentMaintenanceRecord class]]){
        YWEquipmentMaintenanceRecord *maintenanceRecord = (YWEquipmentMaintenanceRecord*)recordObject;
        YWEquipment *equipment = [[YWEquipmentManager shareManager] equipmentWithId:maintenanceRecord.equipmentId];
        equipmentName = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"equipment", nil),equipment.equipmentName.length?equipment.equipmentName:NSLocalizedString(@"none", nil)];
        
        location = equipment ? [[YWBuildingManager shareManager] equipmentLocation:equipment] : nil;
        location = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"location", nil),location.length?location:NSLocalizedString(@"none", nil)];
        timeString = [YWCommonTool showTime:maintenanceRecord.completeTime/1000 andFormatter:KTimeFormatShortTimeDate];
        
    }else if ([recordObject isKindOfClass:[YWMeterReadingRecord class]]){
        YWMeterReadingRecord *meterReadingRecord = (YWMeterReadingRecord*)recordObject;
        equipmentName = [NSString stringWithFormat:@"%@: %.1f",NSLocalizedString(@"reading", nil), meterReadingRecord.thisReadNumber];
        YWEquipment *equipment = [[YWEquipmentManager shareManager] equipmentWithId:meterReadingRecord.equipmentId];
        location = equipment ? [[YWBuildingManager shareManager] equipmentLocation:equipment] : nil;
        location = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"location", nil),location.length?location:NSLocalizedString(@"none", nil)];
        timeString = [YWCommonTool showTime:meterReadingRecord.readingTime/1000 andFormatter:KTimeFormatShortTimeDate];
        
    }else if ([recordObject isKindOfClass:[YWAttendanceRecord class]]){
        YWAttendanceRecord *attendanceRecord = (YWAttendanceRecord*)recordObject;
        equipmentName = attendanceRecord.type == YWAttendanceTypeArrive ? NSLocalizedString(@"arrive", nil) : NSLocalizedString(@"leave", nil);
        NSInteger locationId = attendanceRecord.qrcodeId.length ? [[YWEquipmentManager shareManager]locationIdWithQrcodeId:attendanceRecord.qrcodeId] :attendanceRecord.locationId;
        location = [[YWBuildingManager shareManager] areaLocationWithLocationId:locationId];
        location = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"location", nil),location.length?location:NSLocalizedString(@"none", nil)];
        timeString = [YWCommonTool showTime:attendanceRecord.arriveTime andFormatter:KTimeFormatShortTimeDate];
    }
    
    self.equipmentLabel.text = equipmentName;
    self.locationLabel.text = location;
    self.timeLabel.text = timeString;
}

- (void)setUpSubviews{
    UIView *superView = self.contentView;
    [self.equipmentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(superView.mas_top).offset(8);
        make.left.mas_equalTo(superView.mas_left).offset(kMarginW);
        make.right.mas_equalTo(self.timeLabel.mas_left).offset(-kMarginW);
    }];
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_equipmentLabel.mas_bottom).offset(5);
        make.left.mas_equalTo(superView.mas_left).offset(kMarginW);
        make.right.mas_equalTo(superView.mas_right).offset(-kMarginW);
        make.bottom.mas_equalTo(superView.mas_bottom).offset(-kMarginW);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(superView.mas_top).offset(8);
        make.right.mas_equalTo(superView.mas_right).offset(-kMarginW);
        make.bottom.mas_equalTo(_equipmentLabel.mas_bottom);
        make.width.mas_lessThanOrEqualTo(100);
        make.width.mas_greaterThanOrEqualTo(90);
    }];
}

- (UILabel *)equipmentLabel{
    if (!_equipmentLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        [self.contentView addSubview:label];
        _equipmentLabel = label;
    }
    return _equipmentLabel;
}

- (UILabel *)locationLabel{
    if (!_locationLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        [self.contentView addSubview:label];
        _locationLabel = label;
    }
    return _locationLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = KSmallContentFont;
        label.textColor = [UIColor customTextGrayColor];
        label.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:label];
        _timeLabel = label;
    }
    return _timeLabel;
}

@end
